package com.shenhua.idm.provisioning.service;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.shenhua.idm.provisioning.config.AutoPublish;
import com.shenhua.idm.provisioning.mapper.IWebService;
import com.shenhua.idm.provisioning.xsd.UserEntity;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.jws.WebParam;
import javax.jws.WebService;

@Service("WebService")
@AutoPublish(publishAddress = "query")
@WebService(
        endpointInterface = "com.shenhua.idm.provisioning.mapper.IWebService",
        serviceName = "addAccount",
        targetNamespace = "http://provisioning.idm.shenhua.com/")

public class IWebServiceImp implements IWebService {
    private final Logger logger = LoggerFactory.getLogger(IWebServiceImp.class);
    private String msg = "";
    @Override
    public String addAccount(@WebParam(name = "requestID", targetNamespace = "http://provisioning.idm.shenhua.com/", partName = "data")int requestID,
                           @WebParam(name = "userEntity", targetNamespace = "http://provisioning.idm.shenhua.com/",partName = "data")UserEntity userEntity,
                           @WebParam(name = "timeStamp", targetNamespace = "http://provisioning.idm.shenhua.com/",partName = "data")String timeStamp){
        logger.info("request from webservice: {}, {}, {}", requestID, userEntity, timeStamp);
        Unirest.setTimeouts(1000, 1000);
        //请求的主体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("requestID", requestID);
        jsonObject.put("userEntity", userEntity);
        jsonObject.put("timeStamp", timeStamp);
        HttpResponse<String> response = null;
        try {
            response = Unirest.post("http://tp6.traveller-dev.online/index.php/users/addAccount")
                    .header("Accept", "*/*")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body(jsonObject)
                    .asString();
            if(response.getStatus() == 200){
                msg = "用户添加成功";
            }
        } catch (UnirestException e) {
            e.printStackTrace();
            msg = "用户添加失败";
        }
        return msg;
    }
}
